<!DOCTYPE html > 
<html>
<head> 
<base>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>订单详细管理</title>
	<link rel="stylesheet" href="../../layui/css/layui.css" media="all">
  <style>
    body{
    	margin: 10px;
    }
   	.demo-carousel{
   		height: 300px; 
   		line-height: 300px; 
   		text-align: center;
   	}
  </style>
</head>
<body>
	<div class="demoTable">
	  搜索药品名称：
	  <div class="layui-inline">
	    <input class="layui-input" name="drugName" id="drugName" autocomplete="off">
	  </div>
	  <button class="layui-btn" data-type="reload" id="search">搜索</button>
	  <button class="layui-btn" data-type="getCheckData" id="tianjia">添加完成</button>
	</div>
	
	<table class="layui-hide" id="goods" lay-filter="demo"></table>
	<div class="layui-btn-group demoTable">
  		<button class="layui-btn" data-type="getCheckData" id="add">确认添加</button>
	</div>
	
	<table class="layui-table" id='cx' lay-filter="test"></table>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="../../layui/layui.js"></script>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="../../res/mln/getUrlParam.js"></script>
<script>
	var arr = "";
layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
  var laydate = layui.laydate //日期
  ,laypage = layui.laypage //分页
  layer = layui.layer //弹层
  ,table = layui.table //表格
  ,upload = layui.upload //上传
  ,element = layui.element; //元素操作
 
  //监听Tab切换
  element.on('tab(demo)', function(data){
    layer.msg('切换了：'+ this.innerHTML);
    console.log(data);
  });
	//执行一个 table 实例
	  table.render({
	     elem: '#goods'
	    ,url: '../../FdPoBreakdown/findByfdPoId.action?fdPoId='+getUrlParam("fdPoId") //数据接口
	    ,page: true //开启分页
	    ,method:'post'
	    ,size:'sm'
	    ,cols: [[ //表头
	       {type:'checkbox', fixed: 'left'}
	      ,{field: 'fdPodId', title: '采购明细ID', width:120, fixed: 'left'}
	      ,{field: 'fdPoId', title: '采购订单号', width:120}
	      ,{field: 'drugName', title: '采购药品名称', width:150}
	      ,{field: 'fdItemNumber', title: '物品数量', width:120,edit:'text',sort:"true"}
	      ,{field: 'fdSubtotal', title: '小计', width: 120}
	      ,{field: 'fdItemRemark', title: '物品备注', width: 150,edit:'text'}
	      ,{field: 'fdUnit', title: '物品单位', width: 120}
	      ,{field: 'fdPrice', title: '进价', width: 120}
	      ,{fixed: 'right',title: '操作', width: 140, align:'center', toolbar: '#barDemo'}
	    ]]
	  	,id: 'testReload'
	  });
  
  //执行一个 table 实例
  table.render({
     elem: '#cx'
    ,height: 474
    ,url: '../../DrugModel/dindanfindAll.action' //数据接口
    ,page: true //开启分页
    ,method:'post'
    ,size:'sm'
    ,cols: [[ //表头
       {type:'checkbox',fixed: 'left'}
      ,{field: 'drugId', title: '药品ID', width:120, fixed: 'left'}
      ,{field: 'drugImg', title: '药品图片', width:120}
      ,{field: 'drugBarCode', title: '药品条形码', width:120}
      ,{field: 'drugName', title: '药品名称', width:150}
      ,{field: 'drugPrice', title: '价格', width: 120}
      ,{field: 'isva', title: '是否有效', width: 120}
      ,{field: 'pov', title: '单位', width: 120}
      ,{field: 'dom', title: '研制日期', width: 120}
      ,{field: 'drugPrices', title: '建议零售价', width: 120}
      ,{field: 'drugPinyin', title: '汉语拼音简拼', width: 120}
      ,{field: 'dtName', title: '类型名称', width: 120}
      ,{field: 'speIndications', title: '适应症', width: 120}
    ]]
 	 ,id: 'testRe'
  });
  layui.use('table', function(){
	  var table = layui.table;
	  table.on('checkbox(test)', function(obj){
		    console.log(obj)
		});
	  var $ = layui.$, active = {
	    getCheckData: function(){ //获取选中数据
	      var checkStatus = table.checkStatus('testRe')
	      ,data = checkStatus.data;
	      arr = "";
	      arr = arr+JSON.stringify(data);
	      alert(arr);
	    }
	  };
	  $('#add').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		    if(arr == "[],"){
	            alert("请勾选需要操作的记录");
	        }else{
	        	$.ajax({
                    url:"../../FdPoBreakdown/add.action",
                    data:{"arr":arr,"fdPoId":getUrlParam("fdPoId")},
                    type: "post",
                    dataType:"json",
                    cache:false,
                    async: false,
                    success: function(obj){
                    	table.reload('testReload',{
        					page: {
        					    curr: 1 //重新从第 1 页开始
        					  }
        				});
                    }
                 });
	        }
		});
	  
	//监听单元格编辑
	  table.on('edit(demo)', function(obj){
	    var value = obj.value //得到修改后的值
	    ,data = obj.data //得到所在行所有键值
	    ,field = obj.field; //得到字段
	    alert(field);
	    layer.msg('[ID: '+ data.fdPodId +'] [价格: '+data.fdPrice+']' + field + ' 字段更改为：'+ value);
	    var fdItemNumber;
	    var fdItemRemark;
	    if(field=="fdItemNumber"){
	    	var reg = new RegExp("^[0-9]*$");  
	    	if(reg.test(value)){
	    		fdItemNumber=value;
		    	fdItemRemark=data.fdItemRemark;
	    	}else {
	    		
	    		layer.msg('输入的内容不能含有字母或文本', {
					  icon: 1,
					  time: 1000 //1秒关闭（如果不配置，默认是3秒）
					}, function(){
						table.reload('testReload',{page:{curr:1}});
				});
	    		return;
	    	}
	    	
	    }else if(field=="fdItemRemark"){
	    	fdItemRemark=value;
	    	fdItemNumber=data.fdItemNumber;
	    }
	    var fdPodId=data.fdPodId;
	    var fdPrice=data.fdPrice;
	    alert(fdPodId);
	    alert(fdPrice);
	    alert(fdItemNumber);
	    alert(fdItemNumber);
    	$.ajax({
            url:"../../FdPoBreakdown/update.action",
            data:{"fdPodId":fdPodId,"fdPrice":fdPrice,"fdItemRemark":fdItemRemark,"fdItemNumber":fdItemNumber},
            type: "post",
            dataType:"json",
            cache:false,
            async: false,
            success: function(obj){
				alert("修改成功");
				table.reload('testReload',{
					page: {
					    curr: 1 //重新从第 1 页开始
					  }
				});
            }
         });
	  });
  });
  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值
	    if(layEvent === 'del'){
	      var id=data.fdPodId;
	    
	      layer.confirm('确认删除？', function(index){
	        obj.del(); //删除对应行（tr）的DOM结构
	        layer.close(index);
	        //使用jquery的ajax方式向后台提交数据
			var url = "../../FdPoBreakdown/delete.action";
			var data={fdPodId:id};
				$.post(url,data,function(data){
					
					if(data=="ok"){
						layer.msg('删除成功', {
							  icon: 1,
							  time: 1000 //1秒关闭（如果不配置，默认是3秒）
							}, function(){
							  
						});
						table.reload('testReload',{
        					page: {
        					    curr: 1 //重新从第 1 页开始
        					  }
        				});
					}
					
				});
	      	});
	    }
	  });
	//条件查询
	  var $ = layui.$;
			  
	  $('#search').on('click', function(){
		 var matName = $('#drugName');
		 //执行重载
	     table.reload('testReload', {
	       page: {
	         curr: 1 //重新从第 1 页开始
	       }
	       ,where: {
	    	   drugName: matName.val()
	       }
	     });
	  });
	  
	  $('#tianjia').on('click', function(){
		  alert(getUrlParam("fdPoId"));
        	$.ajax({
                 url:"../../FdPoBreakdown/findAllsl.action",
                 data:{"fdPoId":getUrlParam("fdPoId")},
                 type: "post",
                 dataType:"json",
                 cache:false,
                 async: false,
                 success: function(obj){
                 	alert("成功了");
                 }
              });
		});
});
</script>
</body>
</html>